package com.woniuxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.*;
import com.woniuxy.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (Role)角色表控制层
 *
 * @author zjx
 * @since 2021-05-14 19:42:02
 */
@RestController
@RequestMapping("role")
@CrossOrigin
@Api(value = "角色接口",description = "对角色表进行CRUD")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("根据角色id查询角色信息")
    @GetMapping("selectOne")
    public ResponseEntity selectOne(@ApiParam("角色id") Integer id) {
        Role role = this.roleService.queryById(id);
        return new ResponseEntity(200,"查询单个角色信息成功",role);
    }

    /**
     * 分页查询
     *
     * @param map 封装了分页信息和角色信息
     *
     * @return 分页信息
     */
    @ApiOperation("查询所有角色并分页:传入map")
    @PostMapping("selectAll")
    public ResponseEntity selectAll(@RequestBody Map<String,Object> map){
        int startPage = (int) map.get("startPage");
        int pageSize = (int) map.get("pageSize");
        JSONObject jsonObject=JSONObject.fromObject(map.get("role"));
        Role role = (Role)JSONObject.toBean(jsonObject, Role.class);
        PageInfo<Role> pageInfo = this.roleService.queryAllPage(startPage,pageSize,role);
        return new ResponseEntity(200,"分页查询角色信息成功",pageInfo);
    }
    /**
     * 新增一条角色信息
     *
     * @param role 角色信息
     * @return 角色信息
     */

    @ApiOperation("新增一条角色信息:传入角色对象")
    @PostMapping("addRole")
    public ResponseEntity addRole(@RequestBody Role role){
        String result = this.roleService.insert(role);
        return new ResponseEntity(200, result, null);
    }

    /**
     * 通过id删除角色信息
     *
     * @param id 角色id
     * @return  布尔值,是否成功
     */

    @ApiOperation("删除角色信息:传入角色id")
    @GetMapping("deleteRole")
    public ResponseEntity deleterole(@ApiParam("角色id") int id){
        String result = this.roleService.deleteById(id);
        return new ResponseEntity(200, result, null);
    }

    /**
     * 通过传入的角色信息实体类修改信息,并返回修改后的角色信息实体
     *
     * @param role 角色信息实体类
     * @return 角色信息实体类
     */

    @ApiOperation(("修改角色信息:传入角色对象"))
    @PostMapping(value = "updateRole")
    public ResponseEntity updaterole(@RequestBody Role role){
        String result = this.roleService.update(role);
        return new ResponseEntity(200, result, null);
    }
    @ApiOperation(("通过角色id查讯其菜单:传入角色id"))
    @GetMapping("selectMenuById")
    public ResponseEntity selectMenuById(@ApiParam("角色id") int id){
        List<Menu> menuList = this.roleService.selectMenuById(id);
        return new ResponseEntity(200, "通过角色查询他的菜单成功", menuList);
    }

    @ApiOperation(("通过角色id查讯其权限:传入角色id"))
    @GetMapping("selectPermissionById")
    public ResponseEntity selectPermissionById(@ApiParam("角色id") int id){
        List<Permission> permissionList = this.roleService.selectPermissionById(id);
        return new ResponseEntity(200, "通过角色查询他的菜单成功", permissionList);
    }

}
